<?php

namespace app\love\controller;

use think\Controller;
use think\Db;
use think\facade\Log;
use think\facade\Session;
use think\Request;
use function GuzzleHttp\Promise\promise_for;

class Wen extends Controller
{

    public function index()
    {
//        Log::record('测试日志信息');
        Log::record('丰');
    }


    public function create()
    {
        $arr=\request()->post();
        $arr['create_time']=time();
        $validate=new \app\love\validate\Wen();
        $result = $validate->check($arr);

        if(!$result){
           return json(['code'=>200,'msg'=>$validate->getError()]);
        }
        if (Db::name('wen')->insert($arr)){
            session('title',$arr['title']);
           return json(['code'=>200,'msg'=>'添加成功','data'=>$arr]);
        }else{
            return json(['code'=>201,'msg'=>'添加失败','data'=>null]);
        }

    }
    //详情接口day19.com/love/wen/save/?id=1
    public function save(Request $request)
    {
        $id=$request->param('id');
        $res=Db::name('wen')->where('id',$id)->find($id);
        if ($res){
            return json(['code'=>200,'msg'=>'详情展示成功','data'=>$res]);
        }else{
            return json(['code'=>201,'msg'=>'详情展示失败','data'=>null]);
        }
    }
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    //day19.com/love/wen/update/?id=1
    public function update(Request $request, $id)
    {
        $id=$request->param('id');
        $arr=\request()->post();
        Session::get('title',$arr['title']);
        if (empty($id)){
            return json(['code'=>201,'msg'=>'id不能为空']);
        }
        $res=Db::name('wen')->where('id',$id)->update($arr);
        if ($res){
            return json(['code'=>200,'msg'=>'更新成功','data'=>$arr]);
        }else{
            return json(['code'=>201,'msg'=>'更新失败','data'=>null]);
        }
    }
    //接口删除
    public function delete($id)
    {
        $id=\request()->param('id');
        if (empty($id)){
            return json(['code'=>201,'msg'=>'id不能为空']);
        }
        $res=Db::name('wen')->where('id',$id)->delete($id);
        if ($res){
            return json(['code'=>200,'msg'=>'删除成功','data'=>$res]);
        }else{
            return json(['code'=>201,'msg'=>'删除失败','data'=>null]);
        }
    }
    //api展示
    public function show()
    {
        $data=Db::name('wen')->select();
        return json(['code'=>200,'msg'=>'成功展示','data'=>$data]);
    }
    //api搜索  和展示 day19.com/love/wen/search/?title=妈妈
    public function search(){
        $arr['title']=\request()->param('title');
        $res=Db::name('wen')->where('title','like','%'.$arr['title'].'%')->order('create_time','desc')->select();
        if ($res){
            return json(['code'=>200,'msg'=>'搜索展示成功','data'=>$res]);
        }else{
            return json(['code'=>201,'msg'=>'搜索展示失败','data'=>null]);

        }

    }
    //三级分类的一个优化操作
    public function add(){
        $cate_one=[
            ['id'=>1,'cat_name'=>"电器"],
            ['id'=>2,'cat_name'=>'服装']
        ];
        $cate_two=[
            ['id'=>2,'cat_name'=>'电视','pid'=>1],
            ['id'=>2,'cat_name'=>'电脑','pid'=>1]
        ];
        $tmp_cate_two=[];
        foreach ($cate_two as $v){
            $tmp_cate_two[$v['pid']][]=$v;//pid作为下标

        }
        print_r($tmp_cate_two);

        foreach($cate_one as &$vv){
            $vv['son']=isset($tmp_cate_two[$vv["id"]])?$tmp_cate_two[$vv["id"]]:[];//检测cate_one 中id是否等于cate_two中的pid
        }
        print_r($cate_one);

    }

}
